package com.atguigu.edu.realtime220815.util;

import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname PhoenixUtils
 * @Description TODO
 * @Date 2023/2/15 20:45
 * @Created by lzx
 */
public class PhoenixUtils {
    public static void executorSql(String sql ) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DruidDSUtils.getConnection();
            preparedStatement =  connection.prepareStatement(sql);
            preparedStatement.execute();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if(preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
    public static <T> List<T> queryList(String sql, Class<T> clz){
        //3.创建一个集合，把obj放进去
        List<T> resList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs   = null;

        //phoenix  通过连接池获取连接
        try {
            conn = DruidDSUtils.getConnection();
            //获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //执行sql语句
            rs = ps.executeQuery();
            //处理结果集
            //查询结果不确定就使用while .next（）
            /*+-----+-------+------------+------------+-----------+-------------+
                    | ID  | NAME  | REGION_ID  | AREA_CODE  | ISO_CODE  | ISO_3166_2  |
                    +-----+-------+------------+------------+-----------+-------------+
                    | 1   | 北京    | 1          | 110000     | CN-11     | CN-BJ       |*/
            //定义一个实体类对象，用于封装查询出来的所有记录
            //类 ->表；类中属性->字段；类中实例对象-> 一条记录；这种映射关系叫做ORM
            //2.获取属性字段
            ResultSetMetaData metaData = ps.getMetaData();
            //1.创建对象
            while(rs.next()) {
                T obj = clz.newInstance();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    //拿到列名
                    String columnName = metaData.getColumnName(i);
                    //拿到列的值
                    Object columnValue = rs.getObject(i);
                    //拿到这两个，就拿到一个列对应的值
                    //BeanUtils就可以不用反射了
                    BeanUtils.setProperty(obj,columnName,columnValue);
                }
                resList.add(obj);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            //用连接池需要把所有的都关闭，不用连接池，只需把外部连接关了就可以
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return resList;
    }
}
